package com.demo.webapp.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.apache.commons.fileupload.FileItem;

import com.demo.common.ImageUtils;
import com.demo.core.model.File;

/**
 * 图片上传Servlet
 * 图片存储到DB中
 * 该对象将依赖于CORE包下的File对象和DocumentService服务
 * @author ryuu.kk
 *
 */
public class DBImageUploadServlet extends DBFileUploadServlet {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -3444034420244057204L;

	/**
	 * 初始化
	 * @param config ServletConfig
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	/**
	 * 创建一个File模型对象
	 * @param item org.apache.commons.fileupload.FileItem
	 * @return File模型
	 * @throws IOException 
	 */
	protected File buildFile(FileItem item, Map<String, Object> params, int index) throws IOException {
		
		File file = super.buildFile(item, params, index);
		
		//取得图片
		BufferedImage image = ImageUtils.buildImage(item.getInputStream());
		
		int width = image.getWidth();
		int height = image.getHeight();
	
		//设置图片长宽属性
		file.setWidth(width);
		file.setHeight(height);
		
		return file;
	}
	
	/**
	 * 权限判断
	 * @return 是否有权限
	 */
	protected boolean isPermission() {
		//总是允许
		return true;
	}
}